package org.ims.inv.web.action.contacts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.system.ModelAppDictionary;
import org.ims.inv.model.system.ModelAppUser;
import org.ims.inv.model.system.ModelAppUserProfile;
import org.ims.inv.web.action.BaseAppAction;

import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Action: 通讯录
 * 
 * @author Jeccy.Zhao
 *
 */
public class ContactsAction
extends BaseAppAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ContactsAction.class);

	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 通讯录页面
	 */
	public ActionForward pageContactsIndex (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			ModelAppUserProfile formProfile = new ModelAppUserProfile();
			
			String fullName = request.getParameter("fullName");
			if (UtilString.isNotEmpty(fullName))
			{
				formProfile.setFullName(fullName);
			}
			
			String departmentId = request.getParameter("departmentId");
			if (UtilString.isNotEmpty(departmentId))
			{
				ModelAppDictionary department = new ModelAppDictionary();
				department.setId(departmentId);
				formProfile.setDepartment(department);
			}
			
			PagingBean pagingBean = this.getPagingBean(request);
			PaginationSupport<ModelAppUser> pagingData = 
					this.serviceAppUser.findByUserProfile(formProfile, pagingBean);
			
			//PaginationSupport<ModelAppUserProfile> pagingData = 
			//		this.serviceAppUserProfile.getPagination(criteria, pagingBean);
			
			request.setAttribute("datagrid", pagingData);
			this.outWritePagination(request, pagingBean, pagingData);
			this.outWriteAllParameters(request);
			
			// 查询部门字典
			request.setAttribute("department", this.getDictionariesByType("DICT_PROFILE_DEPT"));
			
			return mapping.findForward("page.contacts.index");
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 通讯录详细页面
	 */
	public ActionForward dialogContactPage (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String id = request.getParameter("id");
			if (this.isObjectIdValid(id))
			{
				ModelAppUser user = this.serviceAppUser.get(id);
				ModelAppUserProfile profile = user.getUserProfile();
				
				this.outWriteAllParameters(request);
				
				// 查询用户资料中相关的数据字典
				request.setAttribute("dictDept", this.getDictionariesByType("DICT_PROFILE_DEPT"));
				request.setAttribute("dictDuty", this.getDictionariesByType("DICT_PROFILE_DUTY"));
				request.setAttribute("dictEducation", this.getDictionariesByType("DICT_PROFILE_EDU"));
				
				request.setAttribute("entity", profile);
				request.setAttribute("account", user);
				
				return mapping.findForward("dialog.contacts.page");
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("用户ID非法."));
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}
	
}
